/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import edu.pdx.cs.guavadata.Channel;
import edu.pdx.cs.guavadata.Transform;
import edu.pdx.cs.guavadata.domain.Domain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author VuTran
 */
public class TableRef extends Operator {

    String table;
    String alias;
    List<String> columns;

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public TableRef(String table, String alias) {
        this.table = table;
        this.alias = alias;
        this.columns = new ArrayList<String>();
        this.exposedColumns = new ArrayList<String>();
        this.exposedDomains = new ArrayList<Domain>();
        this.parent = null;
    }

    public TableRef(String table, String alias, Collection<String> columns,
            Collection<String> columnAliases, Collection<Domain> domains) {
        this.table = table;
        this.alias = alias;
        this.columns = new ArrayList<String>(columns);
        this.exposedColumns = new ArrayList<String>(columnAliases);
        this.exposedDomains = new ArrayList<Domain>(domains);
        this.parent = null;

        try {
            if (exposedDomains.size() != exposedColumns.size())
                throw new Exception("Number of column aliases does not match the number of column domains in table " + alias + ".");
            if (this.columns.size() != exposedColumns.size())
                throw new Exception("Number of column aliases does not match the number of columns in table " + alias + ".");
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
    }

     @Override
    public SqlText visit(Provider qv) {
        return qv.transform(this);
    }

    @Override
    public void visit(Channel c, String table) {
        c.mapAliases(this, table);
    }

    @Override
    public void visit(Transform t, Command c) {
        t.expand(this, c);
    }

    @Override
    public void notifyChange(Operator old, Operator newOp) {
        // nothing to change
        return;
    }

    @Override
    public Operator clone() {
        return new TableRef(table, alias, columns, exposedColumns, exposedDomains);
    }

    public void addColumn(String column, String columnAlias, Domain domain) {
        this.columns.add(column);
        this.exposedColumns.add(columnAlias);
        this.exposedDomains.add(domain);
    }
}
